package com.joysuch.wwyt.patrol.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.core.exception.ApiCoreException;
import com.joysuch.wwyt.patrol.entity.bean.*;
import com.joysuch.wwyt.patrol.entity.dto.AppTaskDto;
import com.joysuch.wwyt.patrol.entity.dto.WebTaskDto;
import com.joysuch.wwyt.patrol.entity.vo.CheckPointDetailVo;
import com.joysuch.wwyt.patrol.entity.vo.WebTaskCheckVo;
import com.joysuch.wwyt.patrol.service.PatrolTaskService;
import com.joysuch.wwyt.repeatsubmit.NoRepeatSubmit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author heqifeng
 * @since 2022-02-28
 */
@Slf4j
@RestController
@RequestMapping("/patrol/patrol-task")
@Api(tags = "智能巡检-检查任务")
public class PatrolTaskController {
    @Autowired
    private PatrolTaskService service;

    @GetMapping("producePatrolTask")
    @ApiOperation(value = "手动生成任务")
    public void producePatrolTask() {
        service.producePatrolTask(null);
    }

    @GetMapping("app-task-list")
    @ApiOperation(value = "app任务列表")
    public ResultBean appGetTask(Long page, Long size) {
        Page<AppTaskDto> iPage = service.appGetTaskList(page, size);
        return ResultBean.pageData(iPage.getRecords(),iPage.getTotal());
    }

    @GetMapping("app-synthesize-list")
    @ApiOperation(value = "app综合排查列表", response = AppSynthesizeBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, paramType = "query", name = "page", value = "当前页码"),
            @ApiImplicitParam(required = true, paramType = "query", name = "size", value = "每页记录数"),
            @ApiImplicitParam(required = false, paramType = "query", name = "status", value = "状态 全部为null 1未开始 2已开始 3已结束"),
            @ApiImplicitParam(required = true, paramType = "query", name = "date", value = "日期"),
            @ApiImplicitParam(required = false, paramType = "query", name = "category", value = "类型1 为巡检 2为隐患排查，默认为null"),
    })
    public ResultBean synthesizeTask(Long page, Long size, Integer status, Date date, Integer category) {
        Page<AppSynthesizeBean> beanPage = service.synthesizeTask(page, size, status, date, category);
        return ResultBean.pageData(beanPage.getRecords(), beanPage.getTotal());
    }

    @GetMapping("web-task-list")
    @ApiOperation(value = "web任务列表")
    public ResultBean webGetTaskList(TaskSearchBean searchBean) {
        Page<WebTaskDto> page = service.webGetTaskList(searchBean);
        return ResultBean.pageData(page.getRecords(),page.getTotal());
    }

    @GetMapping("task-finished")
    @ApiOperation(value = "排查记录查询")
    public ResultBean getTaskRecord(TaskRecordSearchBean searchBean) {
        Page<WebTaskDto> page = service.getTaskHadDone(searchBean);
        return ResultBean.pageData(page.getRecords(),page.getTotal());
    }

    @GetMapping("task-finished-count")
    @ApiOperation(value = "排查记录查询数量")
    public ResultBean getTaskRecordCount(TaskRecordSearchBean searchBean) {
        Integer taskHadDoneCount = service.getTaskHadDoneCount(searchBean);
        return ResultBean.success(taskHadDoneCount);
    }

    @GetMapping("app-check-point-list")
    @ApiOperation(value = "app检查点查询")
    public ResultBean appGetCheckPoint(Long taskId) {
        return ResultBean.success(service.appGetCheckPointByTaskId(taskId));
    }

    @GetMapping("app-check-point-except-list")
    @ApiOperation(value = "app检查点查询(异常)")
    public ResultBean appGetExCheckPointRecord(Long taskId) {
        // app查看记录时，异常的检查点
        return ResultBean.success(service.appGetCheckPointRecordByTaskId(taskId));
    }

    @GetMapping("app-check-item-list")
    @ApiOperation(value = "检查项目查询")
    public ResultBean appGetCheckItem(Long pointId) {
        CheckPointDetailVo itemList = service.getItemByPoint(pointId);
        return ResultBean.success(itemList);
    }

    @GetMapping("app-new-check-item-list")
    @ApiOperation(value = "检查项目查询(带隐患排查)")
    public ResultBean appNewGetCheckItem(Long page, Long size,Long pointId) {
        CheckPointDetailVo itemList = service.getNewItemByPoint(page,size,pointId);
        return ResultBean.success(itemList);
    }


    @GetMapping("web-check-item-list")
    @ApiOperation(value = "检查项目(web)")
    public ResultBean webGetCheckItem(Long taskId) {
        WebTaskCheckVo itemList = service.getItemByTask(taskId);
        return ResultBean.success(itemList);
    }

    @GetMapping("get-monitor")
    @ApiOperation(value = "获取监测点")
    public ResultBean getMonitorByDevice(Long deviceId) {
        List<SimpleMonitoringPointBean> monitor = service.getMonitorByDevice(deviceId);
        return ResultBean.success(monitor);
    }

    @PostMapping("app-do-task")
    @ApiOperation(value = "app完成任务")
    @NoRepeatSubmit(paramNames = "pointId")
    public ResultBean appDoTask(@RequestBody AppFinishPointBean pointBean) {
        try {
            return ResultBean.success(service.appDoTask(pointBean));
        } catch (ApiBusinessException e) {
            e.printStackTrace();
            return ResultBean.fail(e.getCode(), e.getMessage());
        }
    }

    @GetMapping("get-location")
    @ApiOperation(value = "app完成任务")
    public ResultBean getLocation() {
        return ResultBean.success(service.getLocation());
    }

    @PostMapping("web-do-task")
    @ApiOperation(value = "web完成任务")
    @RequiresPermissions("patrol:task:do")
    public ResultBean webDoTask(@RequestBody List<AppFinishPointBean> pointBeans) {
        try {
            return ResultBean.success(service.webDoTask(pointBeans));
        } catch (ApiBusinessException e) {
            e.printStackTrace();
            return ResultBean.fail(e.getCode() ,e.getMessage());
        }
    }

    @GetMapping("app-location-check")
    @ApiOperation(value = "位置校验")
    public ResultBean locationCheck(Long pointId, Long userId) throws ApiCoreException {
        return ResultBean.success(service.position(pointId,userId));
    }


    @GetMapping("get-status")
    @ApiOperation(value = "获取任务状态")
    public ResultBean getTaskStatus (Long taskId) {
        return ResultBean.success(service.getTaskStatus(taskId));
    }

    @GetMapping("export-task")
    @ApiOperation(value = "排查记录导出")
    public void exportTaskRecord(@Valid TaskRecordSearchBean searchBean, HttpServletRequest request, HttpServletResponse
            response) {
        try {
            service.exportTaskRecord(searchBean, request, response);
        } catch (Exception e) {
            log.error("统计列表导出",e);
        }
    }
}

